﻿Public Class WebUploader
    Private m_User As String = ""
    Private m_Password As String = ""
    Private m_StreamName As String = ""
    Private m_Url As String = ""
    Public Sub New()

    End Sub
    Public Sub New(ByVal Url As String)
        m_Url = Url
    End Sub
    Public Sub New(ByVal UserName As String, ByVal Password As String)
        m_User = UserName
        m_Password = Password
    End Sub
    Public Sub New(ByVal StreamName As String, ByVal UserName As String, ByVal Password As String)
        m_StreamName = StreamName
        m_User = UserName
        m_Password = Password
    End Sub
    Public Sub New(ByVal StreamName As String, ByVal Url As String, ByVal UserName As String, ByVal Password As String)
        m_StreamName = StreamName
        m_Url = Url
        m_User = UserName
        m_Password = Password
    End Sub
    Public Property URL() As String
        Get
            Return m_Url
        End Get
        Set(ByVal value As String)
            m_Url = value
        End Set
    End Property
    Public Property UserName() As String
        Get
            Return m_User
        End Get
        Set(ByVal value As String)
            m_User = value
        End Set
    End Property
    Public Property Password() As String
        Get
            Return m_Password
        End Get
        Set(ByVal value As String)
            m_Password = value
        End Set
    End Property
    Public Property StreamName() As String
        Get
            Return m_StreamName
        End Get
        Set(ByVal value As String)
            m_StreamName = value
        End Set
    End Property
    Public Function UploadFile(ByVal FileName As String) As Boolean
        Dim OldValue As Boolean = System.Net.ServicePointManager.Expect100Continue

        Try

            System.Net.ServicePointManager.Expect100Continue = False
            '1. Cookie
            Dim Cookie As New Net.CookieContainer()

            '2. Arguments
            Dim QueryStringArguments As New Dictionary(Of String, String)
            QueryStringArguments.Add("username", m_User)
            QueryStringArguments.Add("password", m_Password)
            QueryStringArguments.Add("streamname", m_StreamName)

            '3. contentType
            Dim ContentType As String = ""
            Select Case IO.Path.GetExtension(FileName).ToLower
                Case ".jpg"
                    ContentType = "image/jpeg"
                Case ".jpeg"
                    ContentType = "image/jpeg"
                Case ".gif"
                    ContentType = "image/gif"
                Case ".png"
                    ContentType = "image/png"
                Case ".bmp"
                    ContentType = "image/bmp"
                Case ".tif"
                    ContentType = "image/tiff"
                Case ".tiff"
                    ContentType = "image/tiff"
                Case Else
                    ContentType = "image/unknown"
            End Select

            '4. Upload and return Rta
            UploadFileEx(FileName, m_Url, "streamdata", ContentType, QueryStringArguments, Cookie)
        Catch ex As Exception
        Finally
            System.Net.ServicePointManager.Expect100Continue = OldValue
        End Try
        Return True
    End Function
    Private Function UploadFileEx(ByVal FileName As String, ByVal URL As String, ByVal FileFormName As String, ByVal ContentType As String, ByVal QueryStringArguments As Dictionary(Of String, String), ByVal Cookies As Net.CookieContainer) As String

        If FileFormName = "" Then FileFormName = "file"
        If ContentType = "" Then ContentType = "application/octet-stream"

        Dim PostData As String = "?"
        If QueryStringArguments IsNot Nothing Then
            For Each kvp As KeyValuePair(Of String, String) In QueryStringArguments
                PostData &= kvp.Key & "=" & kvp.Value & "&"
            Next
        End If


        Dim URI As New Uri(URL + PostData)
        Dim Boundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
        Dim WReq As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(URI), Net.HttpWebRequest)
        WReq.CookieContainer = Cookies
        WReq.ContentType = "multipart/form-data; boundary=" + Boundary
        WReq.Method = "POST"

        Dim PostHeader As String = String.Format("--" & Boundary & "{0}" & _
        "Content-Disposition: form-data; name=""" & FileFormName _
        & """; filename=""" & IO.Path.GetFileName(FileName) & """{0}" _
        & "Content-Type: " & ContentType & "{0}{0}", vbNewLine)

        Dim PostHeaderBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(PostHeader)
        Dim BoundaryBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" + Boundary + vbNewLine)

        Dim FileStream As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)

        WReq.ContentLength = PostHeaderBytes.Length + FileStream.Length + BoundaryBytes.Length

        Dim RequestStream As IO.Stream = WReq.GetRequestStream()
        RequestStream.Write(PostHeaderBytes, 0, PostHeaderBytes.Length)
        Dim buffer As Byte() = New Byte(CInt(Math.Min(4096, CInt(FileStream.Length))) - 1) {}
        Dim bytesRead As Integer = 0
        Do
            bytesRead = FileStream.Read(buffer, 0, buffer.Length)
            If bytesRead = 0 Then Exit Do
            RequestStream.Write(buffer, 0, bytesRead)
        Loop
        RequestStream.Write(BoundaryBytes, 0, BoundaryBytes.Length)

        Dim Rta As Net.WebResponse = WReq.GetResponse()
        Dim s As IO.Stream = Rta.GetResponseStream()
        Dim sr As New IO.StreamReader(s)

        Return sr.ReadToEnd()
    End Function

End Class
